//
//  ProductVM.swift
//  iWatchManager
//
//  Created by GujyHy on 2018/1/24.
//  Copyright © 2018年 GujyHy. All rights reserved.
//

import UIKit

class ProductVM: NSObject {
 
    var product:ProductModel = ProductModel()
}

extension ProductVM {
    
    // MARK: 产品数据
    func requestProductListData(_ id:String,
                                finishedSuccess: @escaping ()->(),
                                finishedFail: @escaping (_ message:String) -> ()){
        
        let url = tURL_Server + tURL_QueryProductInfo
        var parameters:[String:String] = [String:String]()
        parameters["id"] = id
     
        HttpsTools.requestData(.get, URLString:url,parameters: parameters,finishedCallBack:{
            (json) in
            let code = json["head"]["code"].stringValue
            let message  = json["head"]["msg"].stringValue
            
            if code == "0000" {
//                self.noticeModel = nil
//                let arrayNames =  json["body"]["msg"]["items"].arrayValue
//                if arrayNames.count > 0 {
//                    let subJson = arrayNames[0]
//                    self.noticeModel = NoticeModel.init(subJson)
//                }
                finishedSuccess()
            }else{
                finishedFail(message)
            }
            
        },finishedFailure:{
            (errorMessage) in
            finishedFail(errorMessage)
        })
    }
    
    // MARK:上传图片 base64 字符串
    func requestUploadImage(_ image:UIImage,
                            productid:String,
                            typeid:String,
                            finishedSuccess: @escaping ()->(),
                            finishedFail: @escaping (_ message:String) -> ()){
        
        let url             = tURL_Server + tURL_QueryProductInfo
        let imageData       = UIImageJPEGRepresentation(image, 1.0)
        let imageDataString = imageData?.base64EncodedData()
        var parameters:[String:String] = [String:String]()
        parameters["id"] = productid
        
        HttpsTools.requestData(.get, URLString:url,parameters: parameters,finishedCallBack:{
            (json) in
            let code = json["head"]["code"].stringValue
            let message  = json["head"]["msg"].stringValue
            
            if code == "0000" {
                //                self.noticeModel = nil
                //                let arrayNames =  json["body"]["msg"]["items"].arrayValue
                //                if arrayNames.count > 0 {
                //                    let subJson = arrayNames[0]
                //                    self.noticeModel = NoticeModel.init(subJson)
                //                }
                finishedSuccess()
            }else{
                finishedFail(message)
            }
            
        },finishedFailure:{
            (errorMessage) in
            finishedFail(errorMessage)
        })
    }
    
    
    // MARK: 删除图片
    func requestDeleteImage(
                            productid:String,
                            typeid:String,
                            finishedSuccess: @escaping ()->(),
                            finishedFail: @escaping (_ message:String) -> ()){
        
        let url             = tURL_Server + tURL_DeleteImage
        var parameters:[String:String] = [String:String]()
        parameters["id"]     = productid
        parameters["typeid"] = typeid
        
        HttpsTools.requestData(.get, URLString:url,parameters: parameters,finishedCallBack:{
            (json) in
            let code = json["head"]["code"].stringValue
            let message  = json["head"]["msg"].stringValue
            
            if code == "0000" {
                //                self.noticeModel = nil
                //                let arrayNames =  json["body"]["msg"]["items"].arrayValue
                //                if arrayNames.count > 0 {
                //                    let subJson = arrayNames[0]
                //                    self.noticeModel = NoticeModel.init(subJson)
                //                }
                finishedSuccess()
            }else{
                finishedFail(message)
            }
            
        },finishedFailure:{
            (errorMessage) in
            finishedFail(errorMessage)
        })
    }
    
}
